﻿using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.DataConfig;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Config.RazorLib.Services
{
    public class DataConfigService : IDataConfigService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public DataConfigService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }
    
        public async Task<PaginatedResult<DataConfigInfoResponseDto>?> QueryDataConfigsByPage(DataConfigSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.DataConfig.QueryDataConfigs));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<DataConfigInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<DataConfigInfoResponseDto>> UpdateDataConfig(Guid id, DataConfigEditRequestDto editRequestDto)
        {
            throw new NotImplementedException();
        }

        public Task<Result<DataConfigInfoResponseDto>> CreateDataConfig(DataConfigEditRequestDto editRequestDto)
        {
            throw new NotImplementedException();
        }

        public Task<Result<DataConfigInfoResponseDto>> GetDataConfig(Guid id)
        {
            throw new NotImplementedException();
        }

        public async Task<PaginatedResult<ConfigFieldInfoResponseDto>?> QueryDataConfigFieldsByPage(string configCode,
            ConfigFieldSearchRequestDto model)
        {
           try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.DataConfig.QueryFields.Replace("{configCode}",configCode)));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<ConfigFieldInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<IList<ConfigFieldInfoResponseDto>>> QueryDataConfigFields(string configCode,
            ConfigFieldSearchRequestDto searchData)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ConfigFieldInfoResponseDto>> GetDataConfigField(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ConfigFieldInfoResponseDto>> CreateDataConfigField(string configCode,
            ConfigFieldEditRequestDto? editData)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ConfigFieldInfoResponseDto>> UpdateDataConfigField(Guid id, ConfigFieldEditRequestDto? editData)
        {
            throw new NotImplementedException();
        }

        public Task<IList<ConfigColMetaInfoResponseDto>> QueryAvailableColumnByDataType(string configCode)
        {
            throw new NotImplementedException();
        }

        public async Task<PaginatedResult<ConfigRuleInfoResponseDto>?> QueryDataConfigRulesByPage(string configCode,
            ConfigRuleSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.DataConfig.QueryRules.Replace("{configCode}",configCode)));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<ConfigRuleInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<ConfigRuleInfoResponseDto>> GetDataConfigRule(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ConfigFieldInfoResponseDto>> CreateDataConfigRule(string configCode,
            ConfigRuleEditRequestDto? editData)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ConfigFieldInfoResponseDto>> UpdateDataConfigRule(Guid id, ConfigRuleEditRequestDto? editData)
        {
            throw new NotImplementedException();
        }
    }
}